package cn.com.api.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.api.entity.ApiT_goods_activity;
import cn.com.api.mapper.ApiT_goods_activityMapper;
import cn.com.api.mapper.ApiT_productsMapper;
import cn.com.api.resultpage.InitChargePage;
import cn.com.api.service.ApiT_goods_activityService;
import cn.com.api.service.StartOnLoadService;
import cn.com.util.*;
import cn.com.util.*;

/**
 * 
 **/
@Service
public class ApiT_goods_activityServiceImpl implements
		ApiT_goods_activityService {
	
	@Autowired
	private StartOnLoadService startOnLoadService;
	
	@Autowired
	private ApiT_goods_activityMapper apiT_goods_activityMapper;
	@Autowired
	private ApiT_productsMapper productsMapper;

	// 根据id获取
	public ApiT_goods_activity findT_goods_activityById(Integer id) {
		ApiT_goods_activity t_goods_activity = apiT_goods_activityMapper
				.findT_goods_activityById(id);
		return t_goods_activity;
	}

	// 查看数据集
	public List<ApiT_goods_activity> findT_goods_activitys() {
		List<ApiT_goods_activity> t_goods_activitys = apiT_goods_activityMapper
				.findT_goods_activitys();
		return t_goods_activitys;
	}

	// 添加
	public int insertT_goods_activity(ApiT_goods_activity t_goods_activity) {
		int ret_I = apiT_goods_activityMapper
				.insertT_goods_activity(t_goods_activity);
		return ret_I;
	}

	// 修改
	public int updateT_goods_activity(ApiT_goods_activity t_goods_activity) {
		int ret_I = apiT_goods_activityMapper
				.updateT_goods_activity(t_goods_activity);
		return ret_I;
	}

	// 删除
	public int deleteT_goods_activity(Integer id) {
		int ret_I = apiT_goods_activityMapper.deleteT_goods_activity(id);
		return ret_I;
	}

	@Override
	public Map<String, Object> findT_goods_activitysByActTypeId(Integer cityid,
			String act_type_id, PageInfo pi) throws Exception {
		String str = startOnLoadService.dictionaryMap().get(act_type_id
				.toUpperCase());
		List<ApiT_goods_activity> t_goods_activitys = new ArrayList<ApiT_goods_activity>();
		Map<String, Object> res = new HashMap<String, Object>();
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> act = new HashMap<String, Object>();
		if (str != null && !"".equals(str)) {
			//积分商城单写出来
			if("ACT_POINTS_MALL".equals(act_type_id.toUpperCase())){
				t_goods_activitys = apiT_goods_activityMapper.findT_goods_activitysByActTypeIdAndCityIdListPage(cityid, Integer.valueOf(str), pi);
			}else{
				act=apiT_goods_activityMapper.searJin(str, cityid+"");
				if(act==null){
					act=apiT_goods_activityMapper.searOverJin(str, cityid+"");
				}
				if(act!=null){
					t_goods_activitys = apiT_goods_activityMapper
							.findT_goods_activitysByActTypeIdListPage(cityid,
									Integer.valueOf(str),act.get("start_time")+"",act.get("end_time")+"", pi);
				}
			}
			
			if (t_goods_activitys.size() > 0) {
				map.put("start", "0");
				map.put("date", act.get("start_time"));
				map.put("enddate", act.get("end_time"));
				map.put("t_goods_activitys", t_goods_activitys);
				map.put("page", pi);
				res.put("error", "0000");
				res.put(Utils.MESSAGE, "success");
			} else {
				res.put("error", "0000");
				res.put(Utils.MESSAGE, "没有数据！");
			}
			res.put("datas", map);
		} else {
			res.put("error", "-9999");
			res.put(Utils.MESSAGE, "参数错误！");
		}
		return res;
	}

	@Override
	public List<ApiT_goods_activity> groupByAll(String cityid,String actid) {
		List<ApiT_goods_activity> act=new ArrayList<ApiT_goods_activity>();
		act=apiT_goods_activityMapper.groupByAll(cityid,actid);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
		try{
		for(ApiT_goods_activity ac:act){
			Date d1 = sdf.parse(ac.getStart_time());
			Date d2 = sdf.parse(ac.getEnd_time());
			Date d3=new Date();
			boolean flag1 = d1.before(d3);//比较开始时间
			boolean flag2 = d2.before(d3);//比较结束时间
			if(!flag1){//未开始
				ac.setIs_finished(1);
			}else if(flag2){//已结束
				ac.setIs_finished(2);
			}else if(flag1&&!flag2){//进行中
				ac.setIs_finished(3);				
			}	
		}
		}catch (Exception e){
			e.printStackTrace();
		}
		return act;
	}

	@Override
	public List<Map<String, Object>> searActTime(Map<String, String> map) {
		return apiT_goods_activityMapper.searActTime(map);
	}

	@Override
	public boolean updateOver(String isover, String actid) {
		return apiT_goods_activityMapper.updateOver(isover, actid);
	}

	/*
	 *  还原已经结束的活动商品状态价格  
	 * */
	@Override
	@Transactional
	public void updateActStatus() {
		List<Map<String,Object>> acts=new ArrayList<Map<String,Object>>();
		acts=apiT_goods_activityMapper.searOverAct();//act_points_mall
		String jfsc=startOnLoadService.dictionaryMap().get("ACT_POINTS_MALL");//积分商城id
		for(Map<String,Object> act:acts){
			String acttypeid=act.get("act_type_id")+"";
			String actid=act.get("act_id")+"";
			if("-1".equals(acttypeid)||jfsc.equals(acttypeid)){
				apiT_goods_activityMapper.deleteT_goods_activity(Integer.parseInt(actid));
			}else{
				String store=productsMapper.searStoreByProId(act.get("product_id")+"");
				//int num=Integer.parseInt(act.get("oldCount")+"")-(Integer.parseInt(act.get("count")+"")-Integer.parseInt(store));//库存
				//apiT_goods_activityMapper.updateOver("1", act.get("act_id")+"");
				Integer s=Integer.parseInt(act.get("act_id")+"");
				apiT_goods_activityMapper.deleteT_goods_activity(Integer.parseInt(actid));
				productsMapper.updatePriceByPro(act.get("oldPrice")+"", act.get("product_id")+"","");
			}
		}
		
	}

	@Override
	public List<ApiT_goods_activity> searGruopAll(String actid) {
		return apiT_goods_activityMapper.searGruopAll(actid);
	}

	@Override
	@Transactional
	public boolean recoveryPriceAndSNum(Integer actid) {
		ApiT_goods_activity act=new ApiT_goods_activity();
		act=apiT_goods_activityMapper.findT_goods_activityById(actid);
		String store=productsMapper.searStoreByProId(act.getProduct_id()+"");
		//int num=Integer.parseInt(act.getOldcount())-(Integer.parseInt(act.getCount())-Integer.parseInt(store));//库存
		productsMapper.updatePriceByPro(act.getOldprice(), act.getProduct_id()+"","");
		return deleteT_goods_activity(actid)==1;
	}
	
	@Override
	@Transactional
	public void updatePriceAndSNum(Integer actid) {
		ApiT_goods_activity act=new ApiT_goods_activity();
		act=apiT_goods_activityMapper.findT_goods_activityById(actid);
		String store=productsMapper.searStoreByProId(act.getProduct_id()+"");
		//int num=Integer.parseInt(act.getOldcount())-(Integer.parseInt(act.getCount())-Integer.parseInt(store));//库存
		productsMapper.updatePriceByPro(act.getOldprice(), act.getProduct_id()+"","");
	}

	@Override
	public Integer checkActTime(String actid, String cityid, String time,String oldetime,String oldstime,String time1,String charge_money) {
		return apiT_goods_activityMapper.checkActTime(actid, cityid, time,oldetime,oldstime, time1,charge_money);
		
	}

	@Override
	public List<ApiT_goods_activity> searchActivityGoods(
			Map<String, Object> conditions, PageInfo page) {
		return apiT_goods_activityMapper.searchActivityGoodsListPage(conditions, page);
	}
	
	@Override
	public List<Map<String, Object>> searchActivityGoodsInfo(
			Map<String, Object> conditions, PageInfo page) {
		return apiT_goods_activityMapper.searchActivityGoodsInfoListPage(conditions, page);
	}
	
	@Override
	public void searchChargeActivityGoods(Map<String, Object> map,
			String city_id) {
		String act_type_id = "-1";//充值有礼的
		//根据 充值有礼 城市id 当前时间查询符合条件的充值有礼的商品
		List<ApiT_goods_activity> goodsList = apiT_goods_activityMapper.searchChargeActivityGoodsByCityId(act_type_id, city_id);
		List<InitChargePage> chargeList = new ArrayList<InitChargePage>();
		if(goodsList!=null && goodsList.size()>0){
			Map<String,String> chargeMoneyList = new HashMap<String,String>();
			for(ApiT_goods_activity goods: goodsList){
				String charge_money = goods.getCharge_money();
				if(chargeMoneyList.containsKey(charge_money)){//存在
					for(InitChargePage chargePage : chargeList){
						String chargeMoney = chargePage.getCharge_money();
						if(chargeMoney.equals(charge_money)){
							chargePage.getGoodsList().add(goods);
						}
					}
				}else{
					chargeMoneyList.put(charge_money, "");
					List<ApiT_goods_activity> list = new ArrayList<ApiT_goods_activity>();
					list.add(goods);
					InitChargePage chargePage = new InitChargePage();
					chargePage.setCharge_money(charge_money);
					chargePage.setGoodsList(list);
					chargeList.add(chargePage);
				}
			}
		}
		map.put("datas", chargeList);
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "success");
	}
}
